package com.fang.transfer.file;

import io.netty.channel.ChannelHandlerContext;

import java.net.DatagramPacket;

public class ThreadContext {

    private static ThreadLocal<ChannelHandlerContext> channelThreadLocal = new ThreadLocal<>();
    private static ThreadLocal<DatagramPacket> udpPacketThreadLocal = new ThreadLocal<>();


    public static void setUdpPacketThreadLocal(DatagramPacket datagramPacket) {
        udpPacketThreadLocal.set(datagramPacket);
    }

    public static DatagramPacket getUdpPacketThreadLocal() {
        DatagramPacket datagramPacket = udpPacketThreadLocal.get();
        if (datagramPacket == null) {
            throw new RuntimeException("不存在上下文");
        }
        return datagramPacket;
    }


    public static void removeUdpPacketThreadLocal() {
        ThreadContext.udpPacketThreadLocal.remove();
    }

    public static void setChannelHandlerContext(ChannelHandlerContext channelHandlerContext) {
        channelThreadLocal.set(channelHandlerContext);
    }

    public static ChannelHandlerContext getChannelHandlerContext() {
        ChannelHandlerContext channelHandlerContext = channelThreadLocal.get();
        if (channelHandlerContext == null) {
            throw new RuntimeException("不存在上下文");
        }
        return channelHandlerContext;
    }

    public static void removeChannelHandlerContext() {
        channelThreadLocal.remove();
    }

}
